package cn.yj.email.executor;

import cn.yj.email.handle.Email;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;

/**
 * <p>
 *
 * </p>
 *
 * @author 永健
 * @since 2019-06-01 14:09
 */
public abstract class AbstractExecutor extends EmailExecutor
{
    private static Logger logger = LoggerFactory.getLogger(AbstractExecutor.class);
    @Override
    public void send(Email email)
    {
        MimeMessage mimeMessage = javaMailSender().createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(new InternetAddress(email.getFrom(),email.getUserNick(),"utf-8"));
            helper.setText(email.getContent().toString(),true);
            helper.setSubject(email.getSubject().getTitle());
            helper.setTo(email.getRecEmail());
            javaMailSender().send(mimeMessage);
        } catch (MessagingException e) {
            logger.error("发送邮件失败  {}",e.getMessage());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}
